package com.eling.elcms.travel.service.impl;

import com.eling.elcms.basedoc.model.BusinessMessage;
import com.eling.elcms.basedoc.model.TargetType;
import com.eling.elcms.basedoc.model.TaskType;
import com.eling.elcms.basedoc.service.IBusinessMessageManager;
import com.eling.elcms.core.AppContext;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.system.model.CommonUser;
import com.eling.elcms.travel.dao.IAllianceConfirmDao;
import com.eling.elcms.travel.model.*;
import com.eling.elcms.travel.model.PlanItemGroup.CheckInStatus;
import com.eling.elcms.travel.model.view.AllianceConfirmView;
import com.eling.elcms.travel.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AllianceConfirmManagerImpl extends GenericManagerImpl<AllianceConfirm, Long>
		implements IAllianceConfirmManager {

	IAllianceConfirmDao allianceConfirmDao;
	
	@Autowired
	private IAllianceConfirmDetailManager allConfDetManager;
	@Autowired
	private ISignUpRegisterManager signUpRegisterManager;
	@Autowired
	private IGroupReserveManager groupReserveManager;
	@Autowired
	private IPlanItemGroupManager planItemGroupManager;
	
	@Autowired
	private IAllianceSetlDetailManager asdManager;
	
	@Autowired
	private IAllianceSettlementManager allSetlManager;
	@Autowired
 	private IBusinessMessageManager businessMessageManager;

	@Autowired
	public void setAllianceConfirmDao(IAllianceConfirmDao allianceConfirmDao) {
		this.dao = allianceConfirmDao;
		this.allianceConfirmDao = allianceConfirmDao;
	}

	@Override
	public AllianceConfirmView saveAll(AllianceConfirmView cond) {
		AllianceConfirm ac = cond.getAllianceConfirm();

		GroupReserve groupReserve = groupReserveManager.query(cond.getAllianceConfirm().getGroupReserve()).get(0);
		
		ac.setGroupReserve(groupReserve);
		ac=save(ac);
		
		PlanItemGroup planItemGroup = groupReserve.getPlanItemGroup();
		planItemGroup.setCheckInStatus(PlanItemGroup.CheckInStatus.CheckIn);
		planItemGroup = planItemGroupManager.save(planItemGroup);
		// 得到主表数据并保存
		
		// 将主表放入子表中保存
		List<AllianceConfirmDetail> list = cond.getDetail();
		List<AllianceConfirmDetail> returnlist = new ArrayList<AllianceConfirmDetail>();
		List<SignUpRegister> surList = new ArrayList<SignUpRegister>();
		if(!list.isEmpty()){
			surList = list.stream().map(a->a.getSignUpRegister()).collect(Collectors.toList());
		}
		for(SignUpRegister sur : surList){
			sur.setPlanItemGroup(planItemGroup);
		}
		surList = signUpRegisterManager.save(surList);
		for (AllianceConfirmDetail acd : list) {
			for (SignUpRegister s : surList) {
				if (acd.getSignUpRegister().getPkSignUpRegister().equals(s.getPkSignUpRegister())) {
					acd.setSignUpRegister(s);
					acd.setAllianceConfirm(ac);
				}
			}
		}
		returnlist = allConfDetManager.save(list);

		// 如果入住已确认，则推送结算单
		if (ac.getGroupReserve().getPlanItemGroup() != null
				&& ac.getGroupReserve().getPlanItemGroup().getCheckInStatus().equals(CheckInStatus.CheckIn)) {
			asdManager.SendAllSetlDetail(ac);
		}

		// 组装数据
		AllianceConfirmView acv = new AllianceConfirmView();
		acv.setAllianceConfirm(ac);
		acv.setDetail(returnlist);
		
		//推送结算单
		CommonUser cu =(CommonUser)AppContext.curUser();
		AllianceSettlement as=new AllianceSettlement();
		as.setAllianceConfirm(ac);
		as.setOperator(cu);
		as.setConfirmStatus(AllianceSettlement.ConfirmStatus.NoConfirm);
		allSetlManager.save(as);
		businessMessageManager.saveBusinessMessage(new BusinessMessage(TargetType.Role,TaskType.AllianceSettlement,as.getPkAllianceSettlement()));

		return acv;
	}
	
}
